<?php  
	namespace Home\Controller;
	use Think\Controller;
	//就业调查管理
	//@author  程传民
	class ObtainController extends AllowController{
		public function index(){
			//首先拿到此用户的信息

			//先判断用户是否已经填写就业信息
			$desire = D("desire");

			$info = $desire->where(array("stuid"=>$_SESSION['userid']))->find();
			
			if($info){
				//说明用户已经填写
				$this->error("你已经填写过就业信息了",U("User/index"));
			}

			$users = D("users");
			$info1 = $users->where(array("id"=>$_SESSION['userid']))->find();

			//用户所在班级
			$class = D("class");
			$info2 = $class->where(array("id"=>$info1['class']))->find();
			//用户所学的专业
			$major = D("major");
			$info3 = $major->where(array("id"=>$info2['major_id']))->find();
			// var_dump($info3);
			// exit;

			$list = array();
			$i=0;
			foreach($info3 as $key=>$value){
				if($key!="id" && $key!="name" && $key!="addtime" && $value!=null){
					$list[$i][0] = $key;
					$list[$i][1] = $value;
					$i++;
				}
			}

			

			$this->assign("list",$list);
			$this->display("Obtain/index");

		}

		//填入数据
		public function insert(){

			$users = D("users");
			$info1 = $users->where(array("id"=>$_SESSION['userid']))->find();

			$_POST['class'] = $info1['class'];
			$_POST['stuid'] = $_SESSION['userid'];

			$desire = D("desire");

			$inof1 = $desire->add($_POST);

			$this->success("填写成功");

		}

		public function classobtain(){
			$class = D("class");
			$users = D("users");
			$major = D("major");

			//是否按照班级查找
			if(!empty($_GET['class'])){
				$tot = $class->where(array("id"=>$_GET['class']))->where($arr1)->count();
			}else{
				$tot = $class->where($arr1)->count();
			}
			// 分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	$search="";
	    	//是否按照班级查找
			if(!empty($_GET['class'])){
				$list = $class->where(array("id"=>$_GET['class']))->where($arr1)->limit($page->firstRow,$page->listRows)->select();
				$search="/id/{$_GET['class']}";
			}else{
				$list = $class->where($arr1)->limit($page->firstRow,$page->listRows)->select();
			}

			foreach($list as $key=>$value){
				//班主任
				$info1 = $users->where(array("id"=>$value['admin_id']))->find();
				$list[$key]['admin_id'] = $info1['name'];
				//讲师
				$info2 = $users->where(array("id"=>$value['teacher_id']))->find();
				$list[$key]['teacher_id'] = $info2['name'];

				//项目经理
				$info3 = $users->where(array("id"=>$value['pm_id']))->find();
				$list[$key]['pm_id'] = $info3['name'];
				//班级的专业
				$info4 = $major->where(array("id"=>$value['major_id']))->find();
				$list[$key]['major_id'] = $info4['name'];

				//班级状态
				switch($value['status']){
					case 0:
						$list[$key]['status'] = "即将开班";
						break;
					case 1:
						$list[$key]['status'] = "正在上课";
						break;
					case 2:
						$list[$key]['status'] = "已毕业";
						break;
				}

			}

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			//班级列表
			// $class = D("class");
			$classs = $class->select();
			$this->assign("classs",$classs);

			$this->assign("search",$search);
			$this->assign("list",$list);
			$this->display("Obtain/classobtain");
			
		}

		public function download(){
			$desire = D("desire");
			$list = $desire->where(array("class"=>$_GET['classid']))->select();

			//根据班级找出专业
			$major = D("major");
			$class = D("class");
			$info1 = $class->where(array("id"=>$_GET['classid']))->find();

			$info2 = $major->where(array("id"=>$info1['major_id']))->find();

			//寻找班级为$info1['id']的所有学员
			$users = D("users");
			$excel=Array();
			foreach ($list as $key => $value) {
				//学员信息
				$info3 = $users->where(array("id"=>$value['stuid']))->find();
				if($info3['sex'] == 0){
                	$info3['sex'] = "女";
	            }else{
	                $info3['sex'] = "男";
	            }
				$excel[$key]["姓名"] = $info3['name'];
				$excel[$key]["性别"] = $info3['sex'];
				$excel[$key]["年龄"] = date("Y", time()) - substr($info3['card'], 6, 4) + 1;
				$excel[$key]["籍贯"] = $info3['address'];
				$excel[$key]["专业"] = $info3['subject'];
				$excel[$key]["学历"] = $info3['education'];
				$excel[$key]["手机"] = $info3['phone'];
				$excel[$key]["邮箱"] = $info3['email'];
				$excel[$key]["QQ"] = $info3['qq'];
				$excel[$key]["就业方式"] = $list[$key]['obtainMode'];
				$excel[$key]["就业地域"] = $list[$key]['obtainCity'];
				$excel[$key]["就业职位"] = $list[$key]['obtainJob'];
				$excel[$key]["期望薪资"] = $list[$key]['obtainSalary'];
				//班级
				$info4  = $class->where(array("id"=>$value['class']))->find();
				for($i=1;$i<=(count($list[$key])-6);$i++){
					$excel[$key][$info2['question'.$i]] = $list[$key]['question'.$i];
					if($excel[$key][$info2['question'.$i]]==null){
						unset($excel[$key][$info2['question'.$i]]); 
					}
					switch($excel[$key][$info2['question'.$i]]){
						case 1:
							$excel[$key][$info2['question'.$i]] = "精通";
							break;
						case 2:
							$excel[$key][$info2['question'.$i]] = "熟练掌握";
							break;
						case 3:
							$excel[$key][$info2['question'.$i]] = "掌握";
							break;
						case 4:
							$excel[$key][$info2['question'.$i]] = "熟悉";
							break;
						case 5:
							$excel[$key][$info2['question'.$i]] = "了解";
							break;
					}

				}
			}
			$classname  = $class->find($_GET['classid'])["name"];
			PHPExcel(6, rtrim($classname, "班")."班学员就业调查", $excel);
		}

		//清除指定班级的就业信息
		public function delete(){
			$arr = array("class"=>I("post.classid"));

			$desire = D("desire");

			$info = $desire->where($arr)->delete();
			if($info){
				echo "成功";
			}

		}

		// public function download(){
		// 	$desire = D("desire");
		// 	$list = $desire->where(array("class"=>$_GET['classid']))->select();

		// 	//根据班级找出专业
		// 	$major = D("major");
		// 	$class = D("class");
		// 	$info1 = $class->where(array("id"=>$_GET['classid']))->find();

		// 	$info2 = $major->where(array("id"=>$info1['major_id']))->find();

		// 	//寻找班级为$info1['id']的所有学员
		// 	$users = D("users");
		// 	// $i=1;
		// 	foreach ($list as $key => $value) {
		// 		//学员姓名
		// 		$info3 = $users->where(array("id"=>$value['stuid']))->find();
		// 		$list[$key]['stuid'] = $info3['name'];
		// 		//班级
		// 		$info4  = $class->where(array("id"=>$value['class']))->find();
		// 		$list[$key]['class'] = $info4['name'];
				
		// 		for($i=1;$i<=(count($list[$key])-6);$i++){

		// 			$list[$key][$info2['question'.$i]] = $list[$key]['question'.$i];
		// 			unset($list[$key]['question'.$i]); 

		// 			if($list[$key][$info2['question'.$i]]==null){
		// 				unset($list[$key][$info2['question'.$i]]); 
		// 			}

		// 			switch($list[$key][$info2['question'.$i]]){
		// 				case 1:
		// 					$list[$key][$info2['question'.$i]] = "精通";
		// 					break;
		// 				case 2:
		// 					$list[$key][$info2['question'.$i]] = "熟练掌握";
		// 					break;
		// 				case 3:
		// 					$list[$key][$info2['question'.$i]] = "掌握";
		// 					break;
		// 				case 4:
		// 					$list[$key][$info2['question'.$i]] = "熟悉";
		// 					break;
		// 				case 5:
		// 					$list[$key][$info2['question'.$i]] = "了解";
		// 					break;
		// 			}

		// 		}
		// 	}

		// 	var_dump($list);

		// 	exit;
		// }
	}